package com.wei.tang.fragment;

import java.util.List;

import com.wei.tang.AppConest;
import com.wei.tang.R;
import com.wei.tang.adapter.TalkInformationItemAdapter;
import com.wei.tang.base.BaseFragment;
import com.wei.tang.bean.CourseBean;
import com.wei.tang.db.CourseDao;
import com.wei.tang.db.JsonData;
import com.wei.tang.fragment.CourseInformationFragment.LoadClassTask;
import com.wei.tang.ui.course.CourseAcitivity;
import com.wei.tang.ui.me.RegistLoginActivity;
import com.wei.tang.util.Tlog;
import com.wei.tang.widget.EmptyLayout;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class TalkInformationFragment extends BaseFragment {
	private Activity mySelf = null;

	@InjectView(R.id.rl_app_head_left)
	RelativeLayout rl_app_head_left;
	@InjectView(R.id.tv_app_head_left_content)
	TextView tv_app_head_left_content;
	@InjectView(R.id.iv_app_head_left_image)
	ImageView iv_app_head_left_image;
	@InjectView(R.id.rl_app_head_center)
	RelativeLayout rl_app_head_center;
	@InjectView(R.id.tv_app_head_center_content)
	TextView tv_app_head_center_content;
	@InjectView(R.id.iv_app_head_center_image)
	ImageView iv_app_head_center_image;
	@InjectView(R.id.rl_app_head_right)
	RelativeLayout rl_app_head_right;
	@InjectView(R.id.tv_app_head_right_content)
	TextView tv_app_head_right_content;
	@InjectView(R.id.iv_app_head_right_image)
	ImageView iv_app_head_right_image;

	@InjectView(R.id.rl_talk_info_new)
	RelativeLayout rl_talk_info_new;
	@InjectView(R.id.rl_talk_info_hot)
	RelativeLayout rl_talk_info_hot;
	@InjectView(R.id.rl_talk_info_my)
	RelativeLayout rl_talk_info_my;

	@InjectView(R.id.tv_talk_info_new)
	TextView tv_talk_info_new;
	@InjectView(R.id.tv_talk_info_hot)
	TextView tv_talk_info_hot;
	@InjectView(R.id.tv_talk_info_my)
	TextView tv_talk_info_my;

	@InjectView(R.id.lv_talk_new_list)
	ListView lv_talk_new_list;
	@InjectView(R.id.lv_talk_hot_list)
	ListView lv_talk_hot_list;
	@InjectView(R.id.lv_talk_me_list)
	ListView lv_talk_me_list;

	@InjectView(R.id.error_layout_head)
	EmptyLayout error_layout_head;

	private List<CourseBean> newDatas = null;
	private List<CourseBean> hotDatas = null;
	private List<CourseBean> meDatas = null;
	private List<CourseBean> sqlDatas = null;
	private TalkInformationItemAdapter newAdapter = null;
	private TalkInformationItemAdapter hotAdapter = null;
	private TalkInformationItemAdapter meAdapter = null;
	private int titlePosition;

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
			@Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.talk_information_fragment, container, false);
		ButterKnife.inject(this, view);
		initData();
		initView(view);
		return view;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mySelf = activity;
	}

	@Override
	public void initView(View view) {
		tv_app_head_center_content.setText("交流");

		updateTitle();

		lv_talk_new_list.setAdapter(newAdapter);
		lv_talk_new_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(mySelf, CourseAcitivity.class);
				mySelf.startActivity(intent);
			}
		});

		lv_talk_hot_list.setAdapter(hotAdapter);
		lv_talk_hot_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(mySelf, CourseAcitivity.class);
				mySelf.startActivity(intent);
			}
		});

		lv_talk_me_list.setAdapter(meAdapter);
		lv_talk_me_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(mySelf, CourseAcitivity.class);
				mySelf.startActivity(intent);
			}
		});
	}

	@Override
	public void initData() {
		newDatas = JsonData.getNewBeans();
		hotDatas = JsonData.getHotBeans();
		meDatas = JsonData.getMeBeans();

		if (sqlDatas != null) {
			sqlDatas.clear();
		}
		LoadClassTask loadClassTask = new LoadClassTask();
		loadClassTask.execute(0);

		meDatas = JsonData.getMeBeans();
		newAdapter = new TalkInformationItemAdapter(mySelf, newDatas, R.layout.talk_information_fragment_item);
		hotAdapter = new TalkInformationItemAdapter(mySelf, hotDatas, R.layout.talk_information_fragment_item);
		meAdapter = new TalkInformationItemAdapter(mySelf, meDatas, R.layout.talk_information_fragment_item);
		titlePosition = 1;
	}

	class LoadClassTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			CourseDao courseDao = new CourseDao(mySelf);
			sqlDatas = courseDao.findAllCourse();
			if (sqlDatas != null && sqlDatas.size() != 0) {
				msg = "获取成功";
				code = AppConest.WS_SUCCESS;
			} else {
				msg = "获取失败";
				code = AppConest.WS_FALL;
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);

			if (code == AppConest.WS_SUCCESS) {
				meDatas.addAll(sqlDatas);
				meAdapter.notifyDataSetChanged();
			}
		}
	}

	void updateTitle() {
		switch (titlePosition) {
		case 1:
			tv_talk_info_new.setTextColor(activity.getResources().getColor(R.color.green));
			tv_talk_info_hot.setTextColor(activity.getResources().getColor(R.color.text_gray));
			tv_talk_info_my.setTextColor(activity.getResources().getColor(R.color.text_gray));

			lv_talk_new_list.setVisibility(View.VISIBLE);
			lv_talk_hot_list.setVisibility(View.GONE);
			lv_talk_me_list.setVisibility(View.GONE);
			break;
		case 2:
			tv_talk_info_new.setTextColor(activity.getResources().getColor(R.color.text_gray));
			tv_talk_info_hot.setTextColor(activity.getResources().getColor(R.color.green));
			tv_talk_info_my.setTextColor(activity.getResources().getColor(R.color.text_gray));

			lv_talk_new_list.setVisibility(View.GONE);
			lv_talk_hot_list.setVisibility(View.VISIBLE);
			lv_talk_me_list.setVisibility(View.GONE);
			break;
		case 3:
			tv_talk_info_new.setTextColor(activity.getResources().getColor(R.color.text_gray));
			tv_talk_info_hot.setTextColor(activity.getResources().getColor(R.color.text_gray));
			tv_talk_info_my.setTextColor(activity.getResources().getColor(R.color.green));

			lv_talk_new_list.setVisibility(View.GONE);
			lv_talk_hot_list.setVisibility(View.GONE);
			lv_talk_me_list.setVisibility(View.VISIBLE);
			break;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!AppConest.ISLOGIN) {
			error_layout_head.setErrorType(EmptyLayout.NOLOGIN);
		} else {
			error_layout_head.setErrorType(EmptyLayout.HIDE);
		}
	}

	@Override
	@OnClick({ R.id.rl_talk_info_new, R.id.rl_talk_info_hot, R.id.rl_talk_info_my, R.id.error_layout_head })
	public void onClick(View v) {
		Intent intent = null;
		Bundle bundle = null;
		int id = v.getId();
		switch (id) {
		case R.id.rl_talk_info_new:
			if (titlePosition == 1) {
				return;
			}
			titlePosition = 1;
			updateTitle();
			break;
		case R.id.rl_talk_info_hot:
			if (titlePosition == 2) {
				return;
			}
			titlePosition = 2;
			updateTitle();
			break;
		case R.id.rl_talk_info_my:
			if (titlePosition == 3) {
				return;
			}
			titlePosition = 3;
			updateTitle();
			break;
		case R.id.error_layout_head:
			if (!AppConest.ISLOGIN) {
				intent = new Intent(activity, RegistLoginActivity.class);
				activity.startActivity(intent);
			}
			break;
		}
	}
}
